package org.jeecg.glr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.glr.entity.GlrRadio;
import org.jeecg.glr.entity.GlrRadioServer;
import org.jeecg.glr.mapper.GlrRadioMapper;
import org.jeecg.glr.service.IGlrLightPoleDeviceService;
import org.jeecg.glr.service.IGlrRadioServerService;
import org.jeecg.glr.service.IGlrRadioService;
import org.jeecg.glr.service.IGlrVideoYsService;
import org.jeecg.glr.util.GLRConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static org.jeecg.glr.util.GLRConstant.SERVER_CODE_RATTOP;

/**
 * @Description: 音柱管理
 * @Author: jeecg-boot
 * @Date:   2020-02-11
 * @Version: V1.0
 */
@Service
public class GlrRadioServiceImpl extends ServiceImpl<GlrRadioMapper, GlrRadio> implements IGlrRadioService {

    @Resource
    GlrRadioMapper glrRadioMapper;

    @Autowired
    private IGlrLightPoleDeviceService glrLightPoleDeviceService;

    @Autowired
    private IGlrRadioServerService glrRadioServerService;

    @Autowired
    private IGlrVideoYsService glrVideoYsService;

    @Override
    public void saveEntity(GlrRadio glrRadio){
        glrRadioMapper.insert(glrRadio);
        glrLightPoleDeviceService.dataProecss(glrRadio,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_ADD);
    }
    @Override
    public void updateEntityById(GlrRadio glrRadio){
        glrLightPoleDeviceService.dataProecss(glrRadio,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_EDIT);
        glrRadioMapper.updateById(glrRadio);
    }
    @Override
    public void removeEntityById(String id){
        GlrRadio glrRadio=glrRadioMapper.selectById(id);
        glrRadioMapper.deleteById(id);
        glrLightPoleDeviceService.dataProecss(glrRadio,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
    }
    @Override
    public void removeEntityByIds(List<String> asList){
        for(String stId:asList){
            GlrRadio glrRadio=glrRadioMapper.selectById(stId);
            glrRadioMapper.deleteById(stId);
            glrLightPoleDeviceService.dataProecss(glrRadio,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
        }
    }

    @Override
    public List<String> getGlrRadioStatus(){
        String sql="select status from glr_radio where 1=1";
       /* if(!SysUtil.getUserInfo().getUserType().equals(SysConstant.SYS_USER_TYPE_ADMIN)){
                sql=sql+" and sys_company_code='"+SysUtil.getUserInfo().getSysCompanyCode()+"'";
        }*/
        return glrRadioMapper.getStatus(sql);
    }

    @Override
    public void doUpdateStatus() throws Exception{
        List<GlrRadio> radioList = glrRadioMapper.selectList(new QueryWrapper<GlrRadio>());

        GlrRadioServer glrRadioServer=glrRadioServerService.doGetByPropertyName("code", GLRConstant.SERVER_CODE_RATTOP);
        String stJSessionID = glrRadioServerService.getRattopSessionId(glrRadioServer);

        List<String> stListOFFLINE=new ArrayList<>();
        List<String> stListIDLE=new ArrayList<>();
        List<String> stListWORKING=new ArrayList<>();

        for(GlrRadio domain : radioList){
            glrRadioServerService.updateGlrRadioStatus(domain,glrRadioServer,stJSessionID);
            if (domain!=null){
                //glrRadioMapper.updateById(domain);
                if (GLRConstant.RADIO_STATUS_OFFLINE.equals(domain.getStatus())){
                    stListOFFLINE.add(domain.getId());
                }
                if (GLRConstant.RADIO_STATUS_IDLE.equals(domain.getStatus())){
                    stListIDLE.add(domain.getId());
                }
                if (GLRConstant.RADIO_STATUS_WORKING.equals(domain.getStatus())){
                    stListWORKING.add(domain.getId());
                }
            }
        }

        if (stListOFFLINE.size()>0){
            String stSql= org.jeecg.glr.util.StringUtil.joinString1(stListOFFLINE,",");
            stSql="update glr_radio set status='"+GLRConstant.RADIO_STATUS_OFFLINE+"'  where status!='"+GLRConstant.RADIO_STATUS_OFFLINE+"' and id in ("+stSql+")";
            glrVideoYsService.updateSql(stSql);
        }

        if (stListIDLE.size()>0){
            String stSql= org.jeecg.glr.util.StringUtil.joinString1(stListIDLE,",");
            stSql="update glr_radio set status='"+GLRConstant.RADIO_STATUS_IDLE+"'  where status!='"+GLRConstant.RADIO_STATUS_IDLE+"' and id in ("+stSql+")";
            glrVideoYsService.updateSql(stSql);
        }

        if (stListWORKING.size()>0){
            String stSql= org.jeecg.glr.util.StringUtil.joinString1(stListWORKING,",");
            stSql="update glr_radio set status='"+GLRConstant.RADIO_STATUS_WORKING+"'  where status!='"+GLRConstant.RADIO_STATUS_WORKING+"' and id in ("+stSql+")";
            glrVideoYsService.updateSql(stSql);
        }

    }
}
